package com.zzg.contrller;

import java.util.Iterator;

import org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zzg.elastic.entity.Book;
import com.zzg.elastic.repository.BookRepository;

@Controller
public class ElasticSearchContrller {
	@Autowired
	private BookRepository repository;
	
	  //每页数量
    private Integer PAGESIZE=10;
	
	@RequestMapping(value ="/insert", method = RequestMethod.GET)
	@ResponseBody
	public void insert(){
		Book book = new Book(1l, "spring从入门到精通", "创建时间：" + System.currentTimeMillis());
		repository.save(book);
		System.out.println("创建成功");
	}
	
	@RequestMapping(value ="/delete", method = RequestMethod.GET)
	@ResponseBody
	public void delete(@RequestParam Long sid){
		Book book = repository.findById(sid).orElse(null);
		if(book != null){
			repository.delete(book);
			System.out.println("删除成功");			
		}
	}
	
	@RequestMapping(value ="/update", method = RequestMethod.GET)
	@ResponseBody
	public void update(@RequestParam long sid,  @RequestParam String name,  @RequestParam String desc){
		Book book = new Book();
		book.setId(sid);
		book.setName(name);
		book.setDescription(desc);
		repository.save(book);
		System.out.println("更新成功");
	}
	
	@RequestMapping(value ="/selectOne", method = RequestMethod.GET)
	@ResponseBody
	public void selectOne(@RequestParam long sid){
		Book book = repository.findById(sid).orElse(null);
		if(book != null){
			System.out.println("编号:" + book.getId() + ",名称：" + book.getName() + ",描述：" + book.getDescription());
		}
	}
	
	@RequestMapping(value ="/selectList", method = RequestMethod.GET)
	@ResponseBody
	public void selectList(@RequestParam(required = false) Integer pageNumber, @RequestParam String query){
		if(pageNumber==null) pageNumber = 0;
        //es搜索默认第一页页码是0
        SearchQuery searchQuery=getEntitySearchQuery(pageNumber,PAGESIZE,query);
        Page<Book> books = repository.search(searchQuery);
       
        for (Iterator iter = books.iterator(); iter.hasNext();) {
        	Book book = (Book) iter.next();
        	if(book != null){
        		System.out.println("编号:" + book.getId() + ",名称：" + book.getName() + ",描述：" + book.getDescription());
        	}		
        }

	}
	
	private SearchQuery getEntitySearchQuery(int pageNumber, int pageSize, String searchContent) {
		//QueryBuilders.functionScoreQuery(null);
		 QueryStringQueryBuilder builder = new QueryStringQueryBuilder(searchContent);
//        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(builder).
//                .add(QueryBuilders.matchPhraseQuery("name", searchContent),
//                        ScoreFunctionBuilders.weightFactorFunction(100))
//                .add(QueryBuilders.matchPhraseQuery("description", searchContent),
//                        ScoreFunctionBuilders.weightFactorFunction(100))
//                //设置权重分 求和模式
//                .scoreMode("sum")
//                //设置权重分最低分
//                .setMinScore(10);
        
        FilterFunctionBuilder[] functions = {
                new FunctionScoreQueryBuilder.FilterFunctionBuilder(
                		QueryBuilders.matchPhraseQuery("name", searchContent),                 
                		ScoreFunctionBuilders.weightFactorFunction(100)),                   
                new FunctionScoreQueryBuilder.FilterFunctionBuilder(
                		QueryBuilders.matchPhraseQuery("description", searchContent),
                		ScoreFunctionBuilders.weightFactorFunction(100))      
        };
  
        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(functions).scoreMode(FiltersFunctionScoreQuery.ScoreMode.SUM).setMinScore(10);
        // 设置分页
        Pageable pageable = new PageRequest(pageNumber, pageSize);
        return new NativeSearchQueryBuilder()
                .withPageable(pageable)
                .withQuery(functionScoreQueryBuilder).build();
    }

	
	
}
